// xc_lshop/savemoney/index/index.js
var smapidata = require('../model/smapi_data.js')
const app = getApp()

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  
  /**
   * 页面的初始数据
   */
  data: {
    appnav:[
      {val:'',name:"京东",about:"好券商品"},
      {val:'',name:"淘宝",about:"全网好货"},
      {val:'',name:"拼多多",about:"多多推荐"},
      {val:'',name:"唯品会",about:"大牌精选"},
    ],
    CustomBar: app.globalData.CustomBar,
    source:"",
    classcur:"0",
    modellist:[],
    testloading:true,
    rowwidth:['100%','100%'],
    prowidth:150
  },
  xcloaddata: function (options, config) {

    var that=this;
    var update = {};
   
    if (config && config.jtklmapi && config.jtklmapi.home) {
      if(config.jtklmapi.status==1){
        
        update['smapiconfig'] = config.jtklmapi.home
        update['strollconfig'] = config.jtklmapi.stroll

        this.setData(update)
        this.getwidth()

        this.getnavdata()
      }else{
        this.error("功能已关闭",true)
      }
    }else{
      this.error("省钱未配制",true)
    }
    
  },
  //获取宽度
  getwidth(){
    var windowWidth="";
    wx.getSystemInfo({
      success: (result) => {
        console.log(result)
        windowWidth=result['windowWidth']
      },
    })
    var width=(windowWidth - 30) * 0.485 - 16
    width=width.toFixed(2)
    this.setData({
      prowidth:width
    })
  },
  changesource(e){
    console.log(e)
    var dataset = e.currentTarget.dataset;
    this.setData({
      source:dataset.source
    })
    this.getclass(dataset.source)

  },
  clicknav(e){
    var dataset = e.currentTarget.dataset;
    var item=dataset.item
    console.log(item)
    if(item.cid==1){
      wx.navigateTo({
        url:item.url,
      })
    }
    
  },
  onChange(e){
    var  that=this;
    this.setData({
      classid:e.detail.name
    })
    that.getpagelist(that, 1)
  },
  clickpro(e){
    console.log("e")
    var dataset = e.currentTarget.dataset;
    var item=dataset.item
    var id=item.goodsId;
    wx.navigateTo({
      url: '../index/detail?goodsId='+id+'&source='+this.data.source,
    })

    // console.log(e)

  },
  getnavdata(){
    var that=this;
    smapidata.getnavdata().then(function (ret){
      console.log("ret")
      console.log(ret)
      if(ret['status']=1){
        var update={
          platform:ret['data']
        }
        if(ret['data'][0]){
          update['source']=ret['data'][0]['source']
        }
        that.setData(update)
        that.getclass(that.data.source)


      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }

    })
  },
  getclass(source){
    var that=this;
    this.setData({
      class:[]
    })
    smapidata.getclass(source).then(function (ret){
      // console.log("getclass")
      // console.log(ret)
      if(ret['status']=1){
        console.log('getclass')

        console.log(ret['data'])
        var update={
          class:ret['data']
        }
        if(ret['data']['1']){
          update['classid']='1'
        }
        that.setData(update)
        that.getpagelist(that, 1)
        
      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }

    })
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere={};
        strwhere['source']=this.data.source
        strwhere['classid']=this.data.classid

        // strwhere = this.data.searchdata
      }
      that.setData({
        testloading:true,
      });
    }
    smapidata.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
     
         if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"]['data'];
        } else {
          if (result["obj"]["modellist"]['data'].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]['data']);
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      // wx.stopPullDownRefresh();
      //  that.setData({
      //   loading:true,
      //   modellist: []
      // });
      updatemdoel['testloading']=false
      that.setData(updatemdoel);
    }, function () {
  
    });
  },
  afterlogin: function (options, config, userinfo) {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     var that=this;
     that.getpagelist(that,-1)
  },

  
})